﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Input;
using GC_FFW.ForecastWriter.Enums;
using GC_FFW.ForecastWriter.Extensions.Enums;
using GC_FFW.ForecastWriterClient.Helpers;
using GC_FFW.ForecastWriterClient.ViewModels.Parameters;
using GC_FFW.SoccerfameParser.Enums;

namespace GC_FFW.ForecastWriterClient.ViewModels
{
	public class ForecastManagerViewModel : INotifyPropertyChanged
	{
		#region Variables

		private RelayCommand _runCommand;
		private RelayCommand _importSoccerfamePlayerNamesCommand;
		private RelayCommand _importSoccerfameTeamNamesCommand;

		private readonly ForecastManagerViewModelParameters _parameters;

		#endregion

		#region Properties

		public List<KeyValuePair<MatchType, string>> MatchTypes { get; set; }

		public KeyValuePair<MatchType, string> SelectedMatchType { get; set; }

		public string Url { get; set; }

		public ICommand RunButtonCommand
		{
			get { return _runCommand; }
		}

		public ICommand ImportSoccerfamePlayerNamesCommand
		{
			get { return _importSoccerfamePlayerNamesCommand; }
		}

		public ICommand ImportSoccerfameTeamNamesCommand
		{
			get { return _importSoccerfameTeamNamesCommand; }
		}

		#endregion

		#region Constructors

		public ForecastManagerViewModel(ForecastManagerViewModelParameters parameters)
		{
			if (parameters == null)
				throw new ArgumentNullException(string.Format("Forecast Manager View Model Parameters"));
			_parameters = parameters;

			if (parameters.WindowManager == null)
				throw new ArgumentNullException(string.Format("windowManager"));

			FillViewModel();
		}

		#endregion

		#region Internal Implementation

		private void FillViewModel()
		{
			MatchTypes = new List<KeyValuePair<MatchType, string>>();

			foreach (MatchType type in Enum.GetValues(typeof(MatchType)))
			{
				MatchTypes.Add(new KeyValuePair<MatchType, string>(type, type.GetText()));
			}

			SelectedMatchType = MatchTypes.FirstOrDefault();

			_runCommand = new RelayCommand(Run);
			_importSoccerfamePlayerNamesCommand = new RelayCommand(ImportSoccerfamePlayerNames);
			_importSoccerfameTeamNamesCommand = new RelayCommand(ImportSoccerfameTeamNames);
		}

		private void Run(object parameter)
		{
			_parameters.WindowManager.ShowMatchDataEditorWindow(Url, SelectedMatchType.Key);
		}

		private void ImportSoccerfamePlayerNames(object parameter)
		{
			_parameters.WindowManager.ShowImportDataManagerWindow(ImportDataType.SoccerfamePlayerNames);
		}

		private void ImportSoccerfameTeamNames(object parameter)
		{
			_parameters.WindowManager.ShowImportDataManagerWindow(ImportDataType.SoccerfameTeamNames);
		}

		#endregion

		#region INotifyPropertyChanged Members

		public event PropertyChangedEventHandler PropertyChanged;

		protected virtual void OnPropertyChanged(string propertyName)
		{
			var handler = PropertyChanged;
			if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
		}

		#endregion
	}
}
